home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Peter Lewis / PNL Libraries / MyPrinting.p < prev    next >
Encoding:
Text File  |  1994-11-04  |  6.4 KB  |  227 lines  |  [TEXT/PJMM]

  1. unit MyPrinting;
  2.  
  3. interface
  4.  
  5.     uses
  6.         Printing;
  7.  
  8.     type
  9.         PObject = object
  10.                 procedure Create;
  11.                 procedure Destroy;
  12.                 function CountPages (r: rect): integer;
  13.                 function DrawPage (r: rect; gp: GrafPtr; pg: integer; first, last: boolean): OSErr;
  14.                 procedure OpenPrintingStatusDialog;
  15.                 procedure DoIdle;
  16.                 procedure ClosePrintingStatusDialog;
  17.             end;
  18.  
  19.     var
  20.         thePrintingRecordHandle: THPrint;
  21.  
  22.     procedure InitPrinting;
  23.     procedure FinishPrinting;
  24.     function PrintStuff (pob: PObject; thePrRecHdl: THPrint): OSErr; { may return userCanceledErr }
  25.     function DoPageSetup (pob: PObject; thePrRecHdl: THPrint): OSErr;
  26.  
  27. implementation
  28.  
  29.     procedure PObject.Create;
  30.     begin
  31.     end;
  32.  
  33.     procedure PObject.Destroy;
  34.     begin
  35.         dispose(self);
  36.     end;
  37.  
  38.     function PObject.CountPages (r: rect): integer;
  39.     begin
  40.         CountPages := 1;
  41.     end;
  42.  
  43.     procedure PObject.DoIdle;
  44.     begin
  45.     end;
  46.  
  47.     function PObject.DrawPage (r: rect; gp: GrafPtr; pg: integer; first, last: boolean): OSErr;
  48.     begin
  49.         SetPort(gp);
  50.         with r do
  51.             MoveTo((left + right) div 2 - 20, (top + bottom) div 2);
  52.         DrawString('Not Yet Implemented');
  53.         DrawPage := noErr;
  54.     end;
  55.  
  56.     procedure PObject.OpenPrintingStatusDialog;
  57.     begin
  58.         SetCursor(GetCursor(watchCursor)^^);
  59.     end;
  60.  
  61.     procedure PObject.ClosePrintingStatusDialog;
  62.     begin
  63.         SetCursor(arrow);
  64.     end;
  65.  
  66.     var
  67.         gpob: PObject;
  68.  
  69.     procedure DoIdle;
  70.     begin
  71.         gpob.DoIdle;
  72.     end;
  73.  
  74.     function DoPageSetup (pob: PObject; thePrRecHdl: THPrint): OSErr;
  75.         var
  76.             dummy: boolean;
  77.     begin
  78.         PrOpen;
  79.         if PrError = noErr then begin
  80.             dummy := PrStlDialog(thePrRecHdl);
  81.             DoPageSetup := noErr;
  82.         end
  83.         else begin
  84.             DoPageSetup := PrError;
  85.         end;
  86.         PrClose;
  87.     end;
  88.  
  89. {*------ PrintStuff ---------------------------------------------------------*}
  90. {** **   PrintStuff will call all of the necessary Print Manager calls to print }
  91. {**   a document.  It checks PrError() after each Print Manager call.  If an }
  92. { **   error is found, all of the Print Manager open calls (i.e., PrOpen, }
  93. { **   PrOpenDoc...) will have a corresponding close call before the error }
  94. { **   is posted to the user.  You want to use this approach to make sure the }
  95. { **   Print Manager closes properly and all temporary memory is released. }
  96.     function PrintStuff (pob: PObject; thePrRecHdl: THPrint): OSErr;
  97.  
  98.         var
  99.             copies, firstPage, lastPage, loop, numberOfCopies, pageNumber, printmgrsResFile, realNumberOfPagesInDoc: Integer;
  100.             oldPort: GrafPtr;
  101.             thePrPort: TPPrPort;
  102.             theStatus: TPrStatus;
  103.             err: OSErr;
  104.     begin
  105.         GetPort(oldPort);
  106.         gpob := pob;
  107.  
  108.         PrOpen;
  109.         if PrError = noErr then begin
  110.              { Save the current resource file (i.e. the printer driver's) so the driver will not lose its }
  111.             { resources upon return from the pIdleProc.}
  112.             printmgrsResFile := CurResFile;
  113.  
  114.             realNumberOfPagesInDoc := pob.CountPages(thePrRecHdl^^.prInfo.rPage);
  115.  
  116.             if PrJobDialog(thePrRecHdl) then begin
  117.  {                          Get the number of copies of the document that}
  118.  {                          the user wants printed from iCopies of the TPrJob}
  119.   {                         record (IM II-151).}
  120.  
  121.                 numberOfCopies := thePrRecHdl^^.prJob.iCopies;
  122.  
  123.   {                           Get the first and last pages of the document that}
  124.   {                          were requested to be printed by the user from}
  125.    {                         iFstPage and iLastPage from the TPrJob record}
  126.    {                         (IM II-151).}
  127.  
  128.                 firstPage := thePrRecHdl^^.prJob.iFstPage;
  129.                 lastPage := thePrRecHdl^^.prJob.iLstPage;
  130.  
  131. {                             Print "all" pages in the print loop}
  132.  
  133.                 thePrRecHdl^^.prJob.iFstPage := 1;
  134.                 thePrRecHdl^^.prJob.iLstPage := 9999;
  135.                 if (lastPage > realNumberOfPagesInDoc) then
  136.                     lastPage := realNumberOfPagesInDoc;
  137.  
  138.   {                           Print the number of copies of the document}
  139.  {                           requested by the user from the Print Job Dialog.}
  140.                 pob.OpenPrintingStatusDialog;
  141.  
  142.                 for copies := 1 to numberOfCopies do begin
  143.  {                               Install and call your "Print Status Dialog".}
  144.                     thePrRecHdl^^.prJob.pIdleProc := @DoIdle;
  145.  
  146.                     UseResFile(printmgrsResFile);
  147.  
  148.                     thePrPort := PrOpenDoc(thePrRecHdl, nil, nil);
  149.  
  150.                     if (PrError = noErr) then begin
  151.                             {  Print the range of pages of the document requested by the user from the Print Job Dialog.}
  152.  
  153.                         pageNumber := firstPage;
  154.                         while ((pageNumber <= lastPage) and (PrError = noErr)) do begin
  155.  
  156.                             PrOpenPage(thePrPort, nil);
  157.  
  158.                             if (PrError = noErr) then begin
  159.                                 { rPage (IM II-150) is the printable area for the currently selected printer. By passing the current}
  160.                               { enables your app to use the same routine to draw to the screen and the printer's GrafPort.}
  161.  
  162.                                 err := pob.DrawPage(thePrRecHdl^^.prInfo.rPage, GrafPtr(thePrPort), pageNumber, firstPage = pageNumber, lastPage = pageNumber);
  163.                                 if err <> noErr then begin
  164.                                     PrSetError(err);
  165.                                 end;
  166.                             end;
  167.                             PrClosePage(thePrPort);
  168.                             pageNumber := pageNumber + 1;
  169.                         end;  {**  End pagenumber loop  **}
  170.                     end;
  171.                     PrCloseDoc(thePrPort);
  172.                 end;  {**  End copies loop  **}
  173.  
  174.                 pob.ClosePrintingStatusDialog;
  175.   {                            The printing job is being canceled by the request}
  176.   {                            of the user from the Print Style Dialog or the}
  177.   {                            Print Job Dialog PrError will be set to iPrAbort}
  178.    {                           to tell the Print Manager to abort the current}
  179.    {                           printing job.}
  180.             end
  181.             else begin
  182.                 PrSetError(iPrAbort); {** Cancel from the job dialog **}
  183.             end;
  184.         end;
  185.         if (thePrRecHdl^^.prJob.bJDocLoop = bSpoolLoop) and (PrError = noErr) then
  186.             PrPicFile(thePrRecHdl, nil, nil, nil, theStatus);
  187.  
  188.   {        Grab the printing error before you close}
  189.  {        the Print Manager and the error disappears.}
  190.  
  191.         if PrError = iPrAbort then begin
  192.             PrintStuff := userCanceledErr;
  193.         end
  194.         else begin
  195.             PrintStuff := PrError;
  196.         end;
  197.  
  198.         PrClose;
  199.  
  200.         SetPort(oldPort);
  201.     end;  {**  PrintStuff  **}
  202.  
  203.     procedure InitPrinting;
  204.     begin
  205.         thePrintingRecordHandle := THPrint(NewHandle(SIZEOF(TPrint)));
  206.         PrOpen;
  207.         if PrError = noErr then begin
  208.             PrintDefault(thePrintingRecordHandle);
  209.             PrClose;
  210.         end;
  211.     end;
  212.  
  213.     procedure FinishPrinting;
  214.     begin
  215.         DisposHandle(handle(thePrintingRecordHandle));
  216.     end;
  217.  
  218. end.
  219. procedure PObject.PostPrintingErrors (oe: OSErr);
  220.     var
  221.         s: str255;
  222.         a: integer;
  223. begin
  224.     NumToString(oe, s);
  225.     ParamText('Print Error = ', s, '', '');
  226.     a := Alert(fail_alert_id, nil);
  227. end;